home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17628 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  86 lines

  1. Path: news.belwue.de!uzwil!kuehl
  2. From: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: template question
  5. Date: 16 Apr 1996 20:31:58 GMT
  6. Organization: FakultΣt fⁿr Mathematik und Informatik
  7. Message-ID: <4l103u$kmn@news.BelWue.DE>
  8. References: <4l0mk7$4n0@prof.ese-metz.fr>
  9. Reply-To: dietmar.kuehl@uni-konstanz.de
  10. NNTP-Posting-Host: uzwil.informatik.uni-konstanz.de
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Hi,
  14.  
  15. Dominique PELLE (Dominique.Pelle@ingenieurs.supelec.fr) wrote:
  16. : I have a simple problem to solve: 
  17.  
  18. : I would like to have an integer type on n bits and I would like 
  19. : to overload operator+ so that I can use it this way :
  20.  
  21.  
  22. :   integer<7> variable_7bits = 125;
  23. :   integer<4> variable_4bits = 15;
  24. :   integer<7> variable_7bits;
  25.  
  26. :   variable_7bits = variable_7bits + variable_4bits;
  27.  
  28. :   // 'variable_7bits' is now equal to :
  29. :   //  ->  125+15
  30. :   //  ->  140
  31. :   //  ->  12  (because it is on 7 bits)
  32.  
  33.  
  34. :   
  35. : No matter if it is coded on 32 bits, all it has to do, is perform
  36. : operations so that it wraps around n bits AND ALSO detects if the
  37. : operation overflows to warn the user.
  38.  
  39.  
  40. : The question is :
  41. : ~~~~~~~~~~~~~~~
  42.  
  43. : ????????? HOW CAN I WRITE THE OPERATOR+ FUNCTION ??????????
  44.  
  45. Without member templates, you can do something like this:
  46.  
  47.   template <int N>
  48.   class integer
  49.   {
  50.   private:
  51.     // representation
  52.   public:
  53.     int const *representation(); // ugly, because public: unnecessary
  54.                  // with template members...
  55.     integer(int);
  56.     integer(integer<N> const &);
  57.     integer<N> &add(int const *data, int size)
  58.     {
  59.       // do the addition: all information necessary is present
  60.     }
  61.   };
  62.  
  63.   template <int N1, int N2>
  64.   integer<N1> &operator+= (integer<N1> &i1, integer<N2> const &i2)
  65.   {
  66.     return i1.add(i2.representation(), N2);
  67.   }
  68.  
  69.   template <int N1, int N2>
  70.   integer<N1> operator+ (integer<N1> const &i1, integer<N2> const &i2)
  71.   {
  72.     return integer<N1>(i1) += i2;
  73.   }
  74.  
  75. with member templates, you can make 'representation()' private by
  76. adding
  77.  
  78.   template <int M>
  79.   friend integer<N> &operator+= (integer<N> &, integer<M> const &);
  80.  
  81. to the class declaration.
  82. --
  83. dietmar.kuehl@uni-konstanz.de
  84. http://www.informatik.uni-konstanz.de/~kuehl/
  85. I am a realistic optimist - that's why I appear to be slightly pessimistic
  86.